package logic

import (
	"context"
	"errors"
	"gorm.io/gorm"
	"recruit/model"

	"recruit/recruitrpc/internal/svc"
	"recruit/recruitrpc/recruitrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpPasswordLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUpPasswordLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpPasswordLogic {
	return &UpPasswordLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UpPasswordLogic) UpPassword(in *recruitrpc.UpPasswordRequest) (*recruitrpc.UpPasswordResponse, error) {
	// todo: add your logic here and delete this line
	var user model.User
	model.DB.Where("id=?", in.UserId).Find(&user)
	if user.ID == 0 {
		return nil, errors.New("用户未注册")
	}
	if in.Password != in.NewPassword {
		return nil, errors.New("确认密码不一致")
	}
	user = model.User{
		Model:    gorm.Model{ID: uint(in.UserId)},
		Mobile:   in.Mobile,
		Password: in.Password,
	}
	model.DB.Updates(user)
	return &recruitrpc.UpPasswordResponse{
		UserId: int64(user.ID),
	}, nil
}
